<?php
/**
 * Created by PhpStorm.
 * User: CPR137
 * Date: 2018/1/23
 * Time: 14:46
 */

namespace app\modules\admin\controllers;

use app\modules\admin\models\SiteConfig;
use  \app\modules\common\Message;
use app\modules\common\MyController;

class BaseController extends MyController
{
    /**
     * @var 站点配置
     */
    public $siteInfo;

    #禁用csrf
    public $enableCsrfValidation = false;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }

    /**
     * ajax返回 json
     * @param $data
     */
    public function ajaxExit($data)
    {
        exit(json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    /**
     * ajax返回数据
     * @param $code
     * @param $message
     * @param $data
     */
    public function ajaxReturn($code, $message, $data)
    {
        $return = array(
            'status' => $code,
            'msg' => $message,
            'data' => $data,
        );
        $this->ajaxExit($return);
    }


    /**
     * ajax返回数据成功
     * @param string $message
     * @param string $data
     */
    public function ajaxSuccess($message = '执行成功', $data = '')
    {
        $message = !empty($message) ? $message : '执行成功';
        $this->ajaxReturn(1, $message, $data);
    }

    /**
     * ajax返回数据失败
     * @param string $message
     * @param string $data
     */
    public function ajaxFail($message = '执行失败', $data = '')
    {
        $message = !empty($message) ? $message : '执行失败';
        $this->ajaxReturn(0, $message, $data);
    }

    /**
     * 设置消息
     * @param type $url
     */
    public function setMessage($msg)
    {
        return Message::setMessage($msg);
    }

    /**
     * 获取消息
     * @param
     */
    public function getMessage()
    {
        return Message::getMessage();
    }

    /**
     * 获取数据
     * @param
     */
    public function getInfo($key)
    {
        return Message::getInfo($key);
    }

    /**
     *  设置数据
     * @param type $key
     * @param type $value
     */
    public function setInfo($key, $value)
    {
        return Message::setInfo($key, $value);
    }

    /**
     * 删除数据
     * @access public
     * @author furong
     * @param $key
     * @return bool
     * @since  2018年1月25日 09:34:40
     * @abstract
     */
    public function delInfo($key)
    {
        return Message::delInfo($key);
    }

    public function getSiteInfo($key = '')
    {
        $siteConfigModel = new SiteConfig();
        $result = $siteConfigModel->getConfigList('', 'name,value');
        $site_info = [];
        foreach ($result as $value) {
            $site_info[$value['name']] = $value['value'];
        }
        if ($key && isset($site_info[$key])) {
            return $site_info[$key];
        } else {
            return $site_info;
        }
    }


}